package com.qihunet.springbootjwt.service.impl;

import com.qihunet.springbootjwt.config.JwtUser;
import com.qihunet.springbootjwt.persistent.entity.UserInfo;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author gary
 * JWTUserDetailsService implements the Spring Security UserDetailsService interface.
 * It overrides the loadUserByUsername for fetching user details from the database using the username.
 * The Spring Security Authentication Manager calls this method for getting the user details from the database when authenticating the user details provided by the user.
 * Here we are getting the user details from a hardcoded User List
 */
@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if ("gary".equals(username)) {
            return new JwtUser(1L,
                    "M10001",
                    "gary",
                    "$2a$10$slYQmyNdGzTn7ZLBXBChFOC9f6kFjAqPhccnP6DxlWXx2lPk1C3G6",
                    1,
                    new ArrayList<>(),
                    true);
        } else {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }
    }
}
